package org.tea.inventory.admin.web.admin;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.tea.inventory.base.contants.UserConstants;
import org.tea.inventory.base.exception.ApplicationException;
import org.tea.inventory.base.response.BaseResponse;
import org.tea.inventory.base.util.ResponseUtil;
import org.tea.inventory.base.util.StringUtils;
import org.tea.inventory.domain.sys.SysDept;
import org.tea.inventory.service.ISysDeptService;

import java.util.List;

/**
 * 部门信息
 * 
 * @author ruoyi
 */
@Api(tags = "系统管理-部门信息控制器")
@RestController
@RequestMapping("/admin/system/dept")
@RequiredArgsConstructor
public class SysDeptController extends BaseController
{
    @Autowired
    private ISysDeptService deptService;

    /**
     * 获取部门列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "获取部门列表")
    public BaseResponse list(SysDept dept)
    {
        List<SysDept> depts = deptService.selectDeptList(dept);
        return ResponseUtil.ok(depts);
    }

    /**
     * 查询部门列表（排除节点）
     */
    @GetMapping("/list/exclude/{deptId}")
    @ApiOperation(value = "查询部门列表（排除节点）")
    public BaseResponse excludeChild(@PathVariable(value = "deptId", required = false) Long deptId)
    {
        List<SysDept> depts = deptService.selectDeptList(new SysDept());
        depts.removeIf(d -> d.getDeptId().intValue() == deptId || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), deptId + ""));
        return ResponseUtil.ok(depts);
    }

    /**
     * 根据部门编号获取详细信息
     */
    @GetMapping(value = "/{deptId}")
    @ApiOperation(value = "根据部门编号获取详细信息")
    public BaseResponse getInfo(@PathVariable Long deptId)
    {
        return ResponseUtil.ok(deptService.selectDeptById(deptId));
    }


    /**
     * 新增部门
     */
    @PostMapping
    @ApiOperation(value = "新增部门")
    public BaseResponse add(@Validated @RequestBody SysDept dept)
    {
        if (!deptService.checkDeptNameUnique(dept))
        {
            throw new ApplicationException(-1,"新增部门'" + dept.getDeptName() + "'失败，部门名称已存在");
        }
        return ResponseUtil.ok(deptService.insertDept(dept));
    }

    /**
     * 修改部门
     */
    @PutMapping
    @ApiOperation(value = "修改部门")
    public BaseResponse edit(@Validated @RequestBody SysDept dept)
    {
        Long deptId = dept.getDeptId();
        if (!deptService.checkDeptNameUnique(dept))
        {
            throw new ApplicationException(-1,"修改部门'" + dept.getDeptName() + "'失败，部门名称已存在");
        }
        else if (dept.getParentId().equals(deptId))
        {
            throw new ApplicationException(-1,"修改部门'" + dept.getDeptName() + "'失败，上级部门不能是自己");
        }
        else if (StringUtils.equals(UserConstants.DEPT_DISABLE, dept.getStatus()) && deptService.selectNormalChildrenDeptById(deptId) > 0)
        {
            throw new ApplicationException(-1,"该部门包含未停用的子部门！");
        }
        return ResponseUtil.ok(deptService.updateDept(dept));
    }

    /**
     * 删除部门
     */
    @DeleteMapping("/{deptId}")
    @ApiOperation(value = "删除部门")
    public BaseResponse remove(@PathVariable Long deptId)
    {
        if (deptService.hasChildByDeptId(deptId))
        {
            throw new ApplicationException(-1,"存在下级部门,不允许删除");
        }
        return ResponseUtil.ok(deptService.deleteDeptById(deptId));
    }
}
